package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.pojo.User;
import ssm.service.UserService;
import ssm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;


    //登录:分析：登录需要传入username，password，使用Session保存登录信息
    @RequestMapping("/login")
    public String login(HttpSession session,String username,String password)throws Exception{

        System.out.println(username+password+"进入controller！！！");
        //调用查询，是否存在该账号
        User user = userService.findUserByName(username);
        if(user==null){
            System.out.println("没有此用户！！！");
            return "login/sign";//进入注册账号
        }else{
            if(user.getPwd().equals(password)){
                System.out.println("登录成功！！！");
                //session保存用户登录信息
                session.setAttribute("username",user.getName());
                session.setAttribute("userId",user.getId());
                return "forward:home.action";
                //return "success";//跳转到主界面
            }
            else{
                System.out.println("密码错误！！！");
                return "start";//并提示输入错误
            }
        }
    }

    //注册
    @RequestMapping("/sign")
    public String sign(String username,String pwd)throws Exception{

        System.out.println(username + pwd);
        if(username!=null&&pwd!=null){
            System.out.println("输入不为空");
            userService.signUser(username,pwd);
            return "success";
        }

        return "false";
    }

    @RequestMapping("/toLogin")
    public String toLogin()throws Exception{

        return "start";
    }

    //退出
    @RequestMapping("/exit")
    public String exit(HttpSession session)throws Exception{

        //清除session
        session.invalidate();

        //返回登录界面
        return "start";
    }
}
